import { Component, ElementRef, EventEmitter, Injector, Input, OnInit, Output, ViewChild } from '@angular/core';
import { ConfigModule } from 'src/app/config.module';
import { NzMessageService } from 'ng-zorro-antd/message';
import { listAnimation } from '../../../animations/list.animation';
import { SubTableComponent } from '../sub-table/sub-table.component';
import { GlobalConfig } from '../../../common/GlobalConfig';
import {
    EventKeyEnum,
    EventPublishDTO,
    OptionTypeEnum,
    TableOptionModel
} from '../../table/table-option/table-option.model';
import { TableColumnModel } from '../../table/model/tableColumn.model';

@Component({
    selector: 'app-main-table',
    templateUrl: './main-table.component.html',
    styleUrls: ['./main-table.component.scss'],
    animations: [listAnimation]
})
export class MainTableComponent implements OnInit{

    @ViewChild('subTableComponent') _subTableComponent: SubTableComponent;
    @Input()
    searchObj = {
        pageNum: 1,
        pageSize: 15
    };
    @Input()
    tableData: any[];
    @Input()
    tableKey = 'id';
    @Input()
    cateId = 'id';
    @Input()
    subTableKey = 'subId';
    @Input()
    tableColumnArray: TableColumnModel[] ;
    @Input()
    subTableColumnArray: TableColumnModel[] ;
    @Input()
    tableCaseMap: Map<any, Map<string, any>> = GlobalConfig.globalCaseMap;
    @Input()
    mainTableService: any ;
    total: number;
    tableLoading = false;
    //数据服务
    @Input()
    dataService: any;
    @Input()
    subDataService: any;
    scrollValue = { x: '', y: '50vh' };
    editCache: { [key: string]: { edit: boolean; data: any } } = {};
    @Input()
    optionModelList:TableOptionModel[] = null as TableOptionModel[];
    @Input()
    subOptionModelList:TableOptionModel[] = null as TableOptionModel[];
    @Input()
    subOtherOptionModelList:TableOptionModel[] = null as TableOptionModel[];
    @Input()
    tableOptionComponent: any;
    @Input()
    subTableOptionComponent: any;
    @Input()
    refresh: string;
    @Input()
    miniHeight: string = null ;

    constructor(
        private _injector: Injector,
        public _config: ConfigModule,
        private msg: NzMessageService,
        private el: ElementRef
    ) {
    }

    list(){
        this.tableLoading = true;
        this.dataService.list(this.searchObj).subscribe(
            res => {
                if (res.success){
                    this.tableData = res.rows;
                    this.tableData.forEach(item => {
                        item.expand = false;
                    });
                    this.total = res.total;
                }
            }
        ).add( () => {
            this.tableLoading = false;
            this.setMiniTableHeight();
            this.updateEditCache();
        });
    }

    edit(id) {
        this.tableOptionComponent.onModalOpen(id, null);
    }

    ngOnInit(): void {
        this.list();
    }

    setMiniTableHeight() {
        let height = window.innerHeight -300 ;
        this.el.nativeElement.querySelector('.ant-table-body').style.maxHeight = height + 'px';
        this.el.nativeElement.querySelector('.ant-table-body').style.height = height + 'px';
    }

    updateEditCache(): void {
        this.tableData.map(data => {
        data.machinePositionList.map(item => {
                this.editCache[item.machinePositionId] = {
                    edit: false,
                    data: { ...item }
                };
            });
        });
    }

    @Output()
    optionEvent = new EventEmitter();
    @Output()
    subOptionEvent = new EventEmitter();

    protected readonly OptionTypeEnum = OptionTypeEnum;
    protected readonly OptionKeyEnum = EventKeyEnum;

    subEventPublish($event) {
        this.subOptionEvent.emit($event);
    }

    //触发事件
    eventPublish(item: any, eventKey: EventKeyEnum) {
        let data = {
            eventKey: eventKey,
            publishData: item
        } as EventPublishDTO;
        this.optionEvent.emit(data);
    }

    onDelete(item){
        this.dataService.delete(item[this.tableKey]).subscribe(res=>{
            if (res.code == 200){
                this.list()
            }
        })
    }

    //选中按钮
    checkedButtonChange(item:any) {
        if (item.isCheckedButton){
            let data = {
                eventKey: this.OptionKeyEnum.SELECT,
                publishData: item
            } as EventPublishDTO;
            this.optionEvent.emit(data);
        }else {
            let data = {
                eventKey: this.OptionKeyEnum.UNSELECT,
                publishData: item
            } as EventPublishDTO;
            this.optionEvent.emit(data);
        }
    }

    add() {
        let data = {
            eventKey: EventKeyEnum.ADD,
            publishData: null
        } as EventPublishDTO;
        this.optionEvent.emit(data);
    }
}
